/* ====================================================================
 * 作者-------  支叶认知
 * 创作时间---  20220403
 * 功能-------  第三方接口示例（ThirdLib.c 定义应用程序的导出函数）
 * 运行环境---  长沙思知所提供的自然语运行环境
 */
//====================================================================

#include "ThirdBasics.h"
#include "sz_api_ext.h"

//=======dll init=======================
void sz_lib_attach(void* p) { //这里LOG还未完成初始化，不能用LOG输出
	printf("Thirdlib Init...\n");
}

//=======dll deinit===========================
void sz_lib_detach(void* p) {
	printf("...Thirdlib DeInit");
}
//===========================================================================
#include "gfs_use.h"

#define CSP_RAND_FUN

#ifdef CSP_RAND_FUN
#include <Wincrypt.h>
HCRYPTPROV fileTextRnd=NULL;
int GenRandom_Init(int times)
{
	if(fileTextRnd) return 0;
	LPCSTR UserName = "gfstestContainer";
	if(CryptAcquireContext(&fileTextRnd, UserName, NULL, PROV_RSA_FULL, 0)) {
	//	TRACE("A cryptographic context with the %s key container ",UserName);
	//	TRACE("has been acquired.\n\n");
	} 
	else {
		if(GetLastError() == NTE_BAD_KEYSET) {
			if(CryptAcquireContext(&fileTextRnd,UserName,NULL,PROV_RSA_FULL,CRYPT_NEWKEYSET)) {
			//	TRACE("A new key container has been created.\n");
			} 
			else {
				LG("Could not create a new key container.\n");
				return 1;//(1);
			}
		} 
		else {
			LG("A cryptographic service handle could not be ""acquired.\n");
			return 1;
		}
	}
	return 0;
}

void Random_Buff(u8* buff,int len)
{
	if(!CryptGenRandom(fileTextRnd,len, buff))
		LG("##GenRandomBuff failed");
}

u32 Random_Int(void)
{
	u32 rand;
	if(!CryptGenRandom(fileTextRnd,4,(u8*)&rand))
		LG("##GenRandomInt failed");
	return rand;
}

u16 Random_DW(void)
{
	u16 rand;
	if(!CryptGenRandom(fileTextRnd,2,(u8*)&rand))
		LG("##GenRandomDw failed");
	return rand;
}

u8 Random_Byte(void)
{
	u8 rand;
	if(!CryptGenRandom(fileTextRnd,1,&rand))
		LG("##GenRandomByte failed");
	return rand;
}


void GenRandom_Free(void)
{
	 if (!CryptReleaseContext(fileTextRnd,0)) {
		 LG("##The handle could not be released.\n\n");
    }
}
#else

int GenRandom_Init(int times)
{
	while(times--)
		rand();
	return 0;
}

void Random_Buff(u8* buff,int len)
{
	while(len--)
		buff[len]=rand();
}

u32 Random_Int(void)
{
	return rand();
}

u16 Random_DW(void)
{
	return rand();
}

u8 Random_Byte(void)
{
	return rand();
}

void GenRandom_Free(void)
{
}
#endif

//=============================================GFS============================================
//#ifdef GTREE_FILE_COURCE

DfBalancedRecord gAccountHead={0};
u32		gCountdata[32];
u32		gAcountdata[32];

void Test_MsgShow(const char* pTitle,const char* msg)
{
	LG("ShowSta->[%s]--->%s\r\n",pTitle,msg);
}

int gAccount_Init(const char* pTitle)
{
	int ret;
	u32 data[32];
	Test_MsgShow(pTitle,"BalancedAccountInit");

	
	ret=BalancedAccountInit(&gAccountHead,GFS_PAR_ADDR,GFS_PAR_SIZE,sizeof(gAcountdata));
	if(ret > 0) {
		ret=BalancedAccountRead(&gAccountHead,0,data);
		if(data[0]==gAcountdata[0] && data[15]==gAcountdata[15]) {
			LG("gAcountdata[%d][%d] OK.[%d]..Init",data[0],data[15],ret);
		}
		else {
			LG("###gAcountdata[%d,%d][%d,%d] Err[%d]**Init*",gAcountdata[0],data[0],gAcountdata[15],data[15],ret);
		}
	}
	else {
		gAcountdata[0]=0;
		gAcountdata[1]=10;
		gAcountdata[2]=100;
		gAcountdata[3]=1000;
		gAcountdata[4]=10000;
		gAcountdata[15]=100000;
		LG("Init Acountdata NULL****");
	}
	return 0;
}

int gAccount_Run(int Max)
{
	int ret,times,errs=0;
	for(times=0;times<Max;times++) {
		{
			gAcountdata[0]++;
			gAcountdata[1]++;
			gAcountdata[2]++;
			gAcountdata[3]++;
			gAcountdata[4]++;
			gAcountdata[15]+=7;
			ret=BalancedAccountSave(&gAccountHead,gAcountdata);
			if(ret != sizeof(gAcountdata)) {
				LG("###AccountSave ret[%d]Err [%d]*",ret,sizeof(gAcountdata));
				errs++;
			}
		}

		{
			u32 data[32];
			ret=BalancedAccountRead(&gAccountHead,0,data);
			if(memcmp_u32(data,gAcountdata, sizeof(data)/4) )
			{
				LG("###gAcountdata[%d,%d][%d,%d] Err[%d]**run*",gAcountdata[0],data[0],gAcountdata[15],data[15],ret);
				errs++;
			}
		}
	}
	return errs;
}


int EXP_TreeFileInit(EnvP pENV, ExpUnit*& pInOutPar)
{
	if(SPI_Flash_init()) {
		LG("SPI Flash_init ERR\r\n");
	}
	GenRandom_Init(7);
	gAccount_Init("AcFile_Init");
	sz_set_unit_bool(pInOutPar,TRUE);
	return 1;
}

int EXP_TreeFileRun(EnvP pENV, ExpUnit*& pInOutPar)
{
	int Num=10;
	ExpUnit* pUnit = sz_parsing_unit(pInOutPar);
	if (pUnit != NULL && pUnit->type == TYPE_NUM) {
		Num = SZ_GET_INT_VALUE(pUnit);
	}	
	Num=gAccount_Run(Num);
	sz_set_unit_int(pInOutPar,Num);
	return 1;
}

int EXP_TreeFileAuto(EnvP pENV, ExpUnit*& pInOutPar)
{
	int i,Num=10,max,len;
	int ret,Err;
	ExpUnit* pUnit = sz_parsing_unit(pInOutPar);
	if (pUnit != NULL && pUnit->type == TYPE_NUM) {
		Num = SZ_GET_INT_VALUE(pUnit);
	}	
	max = 0;
	Err = 0;
	for(i=0;i<Num;i++) {
		if((Random_DW()%27) == 0) {
			ret=BalancedAccountInit(&gAccountHead,GFS_PAR_ADDR,GFS_PAR_SIZE,sizeof(gAcountdata));
			if(ret==0) {
				LG("###gft_Init..[%d] Err",ret);
			}
			else {
				u32 data[32];
				ret=BalancedAccountRead(&gAccountHead,0,data);
				if(memcmp_u32(data,gAcountdata, sizeof(data)/4)) {
					LG("###Init gAcountRead[%d,%d][%d,%d] Err[%d]**run*",gAcountdata[0],data[0],gAcountdata[15],data[15],ret);
				}
			}
		}
		else {
			len = Random_Int()&0x1ff;
			ret=gAccount_Run(len);
			Err += ret;
			max += len;
		}
	}
	LG("gft_auto[%d] Err[%d] End",max,Err);
	sz_set_unit_int(pInOutPar,max);
	sz_set_unit_int(pInOutPar->pNext,Err);
	return 2;
}

int EXP_FileSysInit(EnvP pENV, ExpUnit*& pInOutPar)
{
	Test_MsgShow("FileSysInit","SPI Flash_init");
	if(SPI_Flash_init()) {
		LG("SPI Flash_init ERR\r\n");
	}
	Test_MsgShow("FileSysInit","SPI_Flash_Exist");
	if(SPI_Flash_Exist()) {
		LG("SPI_Flash_Exist ERR\r\n");
	}
	gfs_Init(GFS_FILE_START,GFS_FILE_SIZE,GFS_FILE_HDNM,0);
	
	ExpUnit* pUnit = sz_parsing_unit(pInOutPar);
	if(pUnit != NULL && pUnit->type==TYPE_DATA) {
		if(gfs_strcmp_path(pUnit->pBfS->pBuf,(u8*)"123456",'\0')>=0) {
			LG(">>>>>>>>>Fomat FileSystem<<<<<<<<<<<\r\n");
			gfs_Fomat(pUnit->pBfS->pStr);
		}
	}
	sz_set_unit_bool(pInOutPar,TRUE);
	return 1;
}

int EXP_FileSysDeinit(EnvP pENV, ExpUnit*& pInOutPar)
{
	Test_MsgShow("FileSysDeinit","SPI Flash_Deinit");
	SPI_Flash_Deinit();
	LG("SPI_Flash_Exit Ok\r\n");
	sz_set_unit_bool(pInOutPar,TRUE);
	return 1;
}

int EXP_FileSysOpen(EnvP pENV, ExpUnit*& pInOutPar)
{
	ExpUnit* pUnit = sz_parsing_unit(pInOutPar);
	if (pUnit == NULL || pUnit->type != TYPE_DATA ) return -5;
	char *pPathName = pUnit->pBfS->pStr;
	pUnit = sz_parsing_unit(pInOutPar->pNext);
	int flag = 0;
	if (pUnit != NULL && pUnit->type == TYPE_NUM) {
		flag = SZ_GET_INT_VALUE(pUnit);
	}	
	gFILE pFd_gfs=gfs_open(pUnit->pBfS->pStr,flag);
	if(pFd_gfs) {
		LG("FileSysOpen[%s]OK,GetSize[%d]\r\n",pUnit->pBfS->pStr,gfs_GetSize(pFd_gfs));
	}
	sz_set_unit_ptr(pInOutPar,pFd_gfs);
	return 1;
}

int EXP_FileSysWrite(EnvP pENV, ExpUnit*& pInOutPar)
{
	ExpUnit* pUnit = sz_parsing_unit(pInOutPar);
	if (pUnit == NULL || pUnit->type != TYPE_NUM || pUnit->tval != NUM_POI) return -5;
	gFILE pFd_gfs=(gFILE)pUnit->uPit;
	pUnit = sz_parsing_unit(pInOutPar->pNext);
	if (pUnit == NULL || pUnit->type != TYPE_DATA ) return -5;
	int ret=gfs_write(pFd_gfs,pUnit->pBfS->pStr,pUnit->pBfS->Len);
	sz_set_unit_int(pInOutPar,ret);
	return 1;
}


int EXP_FileSysRead(EnvP pENV, ExpUnit*& pInOutPar)
{
	ExpUnit* pUnit = sz_parsing_unit(pInOutPar);
	if (pUnit == NULL || pUnit->type != TYPE_NUM || pUnit->tval != NUM_POI) return -5;
	gFILE pFd_gfs=(gFILE)pUnit->uPit;
	pUnit = sz_parsing_unit(pInOutPar->pNext);
	if (pUnit == NULL || pUnit->type != TYPE_NUM) return -5;
	int len = SZ_GET_INT_VALUE(pUnit);
	DataBufStr* pBfs = sz_get_dbs(NULL,len);
	len = gfs_read(pFd_gfs,pBfs->pBuf,pBfs->Len);
	if(len > 0) {
		pBfs->Len = len;
		sz_take_unit_dbs(pInOutPar,DATA_BUF,pBfs);
	}
	else {
		sz_set_unit_bool(pInOutPar,false);
	}
	return 1;
}

int EXP_FileSysSeek(EnvP pENV, ExpUnit*& pInOutPar)
{
	ExpUnit* pUnit = sz_parsing_unit(pInOutPar);
	if (pUnit == NULL || pUnit->type != TYPE_NUM || pUnit->tval != NUM_POI) return -5;
	gFILE pFd_gfs=(gFILE)pUnit->uPit;
	pUnit = sz_parsing_unit(pInOutPar->pNext);
	if (pUnit == NULL || pUnit->type != TYPE_NUM) return -5;
	int offset = SZ_GET_INT_VALUE(pUnit);
	pUnit = sz_parsing_unit(pInOutPar->pNext->pNext);
	if (pUnit == NULL || pUnit->type != TYPE_NUM) return -5;
	int whence = SZ_GET_INT_VALUE(pUnit);
	int ret = gfs_seek(pFd_gfs, offset,whence);
	sz_set_unit_int(pInOutPar,ret);
	return 1;
}

int EXP_FileSysClose(EnvP pENV, ExpUnit*& pInOutPar)
{
	ExpUnit* pUnit = sz_parsing_unit(pInOutPar);
	if (pUnit == NULL || pUnit->type != TYPE_NUM || pUnit->tval != NUM_POI) return -5;
	gFILE pFd_gfs=(gFILE)pUnit->uPit;
	int ret=gfs_close(pFd_gfs);
	sz_set_unit_int(pInOutPar,ret);
	return 1;
}

int EXP_FileSysRename(EnvP pENV, ExpUnit*& pInOutPar)
{
	ExpUnit* pOld = sz_parsing_unit(pInOutPar);
	if (pOld == NULL || pOld->type != TYPE_DATA) return -5;
	ExpUnit* pNew = sz_parsing_unit(pInOutPar->pNext);
	if (pNew == NULL || pNew->type != TYPE_DATA) return -5;
	int ret = gfs_rename(pOld->pBfS->pStr, pNew->pBfS->pStr);
	sz_set_unit_int(pInOutPar, ret);
	return 1;
}

int EXP_FileSysRemove(EnvP pENV, ExpUnit*& pInOutPar)
{
	ExpUnit* pUnit = sz_parsing_unit(pInOutPar);
	if (pUnit == NULL || pUnit->type != TYPE_DATA) return -5;
	int ret=gfs_remove(pUnit->pBfS->pStr);
	sz_set_unit_int(pInOutPar,ret);
	return 1;
}
int EXP_FileSysCheck(EnvP pENV, ExpUnit*& pInOutPar)
{
	int ret=gfs_checkALL();
	sz_set_unit_int(pInOutPar,ret);
	return 1;
}



typedef struct
{
	char sName[32-4];	
	u16 num,flag;
	u32 times;
	u32 fLen;	
	u8* pBuff;			//浼樺厛绾?
}DfTestFS;

#define TEST_FINE_NUM_MAX	GFS_FILE_MAX		//124
#define MAX_SAVE_TREM_LEN	0x3FFF

#define MAX_SAVE_FILE_LEN	(20*1024)	//65535	//(2*1024*1024)
static DfTestFS tfileArr[TEST_FINE_NUM_MAX]={0};

#ifdef GFS_RECORD_FUNTION
int contFileAuto(gFILE pfd,u8 *pBuff,int len,int Maxs)
{
	int i,ret,times,errs=0;
	u8 *pcheak;
	pcheak=pBuff+DfGetBeiSu(len,4);
	for(times=0;times<Maxs;times++) {
		for(i=0;i<len;i++)
			pBuff[i] += i;

		ret=gfs_write(pfd,pBuff,0);
		if(ret < len) {
			LG("###AccountSave ret[%d<%d]Err",ret,len);
			errs++;
		}
		else {
			ret=gfs_read(pfd,pcheak,0);
			if(ret < len) {
				LG("###AccountRead ret[%d<%d]Err",ret,len);
				errs++;
			}
 			if(memcmp(pcheak,pBuff, ret) ) {
				LG("###gAcountdata[%d,%d][%d,%d] Err[%d]**run*",pBuff[0],pcheak[0],pBuff[ret-1],pcheak[ret-1],ret);
				errs++;
			}
		}
	}
	return errs;
}
#endif
void test_filecont(int timesMax)
{
	u16 i,max;
	int time1,time2;
	int ret,times,errs=0;
	u8 testData[32],pcheak[32];
	gFILE pfd=NULL;
	
	#ifdef GTREE_FILE_ERR_RECORD
	LG("test_filecont>(%d,%d)%d,start", timesMax,gfs_erase_times);
	gfs_erase_times=0;
	#endif
	LG("璁板綍鏂囦欢娴嬭瘯...");
	memset(testData,0x00,sizeof(testData));
	times=timesMax;
	time1=GetTickCount();
	max = sizeof(testData)/sizeof(testData[0]);
	pfd=API_fopen("鍥界璁板綍.cnt",F_RDWR|F_CREAT|F_CONT,sizeof(testData),300);
	if(pfd) {
		while(times--) {
			for(i=0;i<max;i++)
				testData[i] += i;
			ret=gfs_write(pfd,testData,sizeof(testData));
			if(ret < sizeof(testData)) {
				LG("###AccountSave ret[%d<%d]Err",ret,sizeof(testData));
			}
			else {
				ret=gfs_read(pfd,pcheak,0);
 				if(ret < sizeof(testData)) {
					LG("###AccountRead ret[%d<%d]Err",ret,sizeof(testData));
					errs++;
				}
				if(memcmp(pcheak,testData, ret) ) {
					LG("###11gAcountdata[%d,%d][%d,%d] Err[%d]**run*",testData[0],pcheak[0],testData[ret-1],pcheak[ret-1],ret);
					errs++;
				}
			}
		}
		gfs_close(pfd);
	}
	
	time1=GetTickCount()-time1;
	gfs_remove("鍥界璁板綍.cnt");
	//-----------------------------------------
	times=timesMax;
	LG("鏅€氭枃浠舵祴璇?..");
	memset(testData,0x00,sizeof(testData));
	time2=GetTickCount();
	max = sizeof(testData)/sizeof(testData[0]);
	pfd=API_fopen("鍥界璁板綍.bin",F_RDWR|F_CREAT);
	if(pfd) {
		while(times--) {
			for(i=0;i<max;i++)
				testData[i] += i;
			gfs_seek(pfd,0,F_SEEK_SET);
			ret=gfs_write(pfd,testData,sizeof(testData));
			if(ret < sizeof(testData)) {
				errs++;
				LG("###fileSave ret[%d<%d]Err",ret,sizeof(testData));
			}
			else {
				gfs_sync(pfd);
				gfs_seek(pfd,0,F_SEEK_SET);
				ret=gfs_read(pfd,pcheak,sizeof(pcheak));
				if(ret < sizeof(testData)) {
					LG("###fileRead ret[%d<%d]Err",ret,sizeof(testData));
					errs++;
				}
				if(memcmp(pcheak,testData, ret) ) {
					LG("###11filedata[%d,%d][%d,%d] Err[%d]**run*",testData[0],pcheak[0],testData[ret-1],pcheak[ret-1],ret);
					errs++;
				}
			}
		}
		gfs_close(pfd);
	}
	time2=GetTickCount()-time2;
	gfs_remove("鍥界璁板綍.bin");
	LG("timesMax[%d],time1[%d],time2[%d],erase[%d],errs[%d]",timesMax,time1,time2,gfs_erase_times,errs);
	gfs_checkALL();
}

#define FILE_TEST_LEN	(GFS_BLOCK_SIZE)
#define FILE_TEST_OFS	(GFS_BLOCK_SIZE/2)

/*
int test_fileSave(int timesMax)
{
	int time1,time2,errs=0;
	u8 *pTestData,*pTestCheak;
	#ifdef GTREE_FILE_ERR_RECORD
	TRACE("test_fileSave>(%d)eg[%d],ed[%d],start[%d]",timesMax,gfs_erase_Global,gfs_erase_times,FILE_TEST_OFS);
	gfs_erase_times=0;
	#endif
	time1=GetTickCount();
	pTestData=malloc(FILE_TEST_LEN);
	pTestCheak=malloc(FILE_TEST_LEN);
	memset(pTestData,0,FILE_TEST_LEN);
	APP_FileSaveBuff("鍥界璁板綍.par",pTestData,(FILE_TEST_LEN));
	time1=GetTickCount()-time1;
	time2=GetTickCount();
	while(timesMax--)
	{
		APP_FileReadBuff("鍥界璁板綍.par",pTestCheak,(FILE_TEST_LEN));
		if(memcmp(pTestData,pTestCheak,FILE_TEST_LEN))
		{
			errs++;
			TRACE("###fileCheck times[%d]Err[%d]Err",timesMax,errs);
		}
		Random_Buff(pTestData+FILE_TEST_OFS,128);
		APP_FileSaveBuff("鍥界璁板綍.par",pTestData,FILE_TEST_LEN);
	}
	time2=GetTickCount()-time2;
	gfs_remove("鍥界璁板綍.par");
	TRACE("time1[%d],time2[%d],eg[%d],ed[%d],errs[%d]",time1,time2,gfs_erase_Global,gfs_erase_times-gfs_erase_Global,errs);
	gfs_checkALL();
	return 1;
}
*/

int EXP_FileSysAutoTest(EnvP pENV, ExpUnit*& pInOutPar)
{
	u8	si,sMax;
	u16 i,max,fIndex,oflag;
	int ret,offset,SaveLen;
	int time1,time2,ets;
	u32 it,times,uRand,errTimes;
	u32 totalSave,totalRead,fileLen;
	u8* pCheckbuff,*pSavebuff;
	gFILE pfd=NULL;
	ExpUnit* pUnit = sz_parsing_unit(pInOutPar);
	if (pUnit != NULL && pUnit->type == TYPE_NUM) {
		times = SZ_GET_INT_VALUE(pUnit);
	}
	else times=7;
	GenRandom_Init(times);
	//test_filecont(times);
	//if(test_fileSave(times))
	//	return pInPar;
	max=sizeof(tfileArr)/sizeof(tfileArr[0]);
	#ifdef GTREE_FILE_ERR_RECORD
	LG("--->(%d,%d)%d,start",times,max,gfs_erase_times);
	gfs_erase_times=0;
	#endif
	for(i=0;i<max;i++) {
		tfileArr[i].sName[0]='\0';
		tfileArr[i].fLen=0;
		tfileArr[i].pBuff=(u8*)malloc(MAX_SAVE_FILE_LEN);
		if(tfileArr[i].pBuff==NULL) {
			LG("###gfs (%d)malloc Err",i);
			return NULL;
		}
	}
	pSavebuff = (u8*)malloc(MAX_SAVE_FILE_LEN);
	totalSave=0;
	fileLen =0;
	totalRead=0;
	errTimes=0;
	time1=GetTickCount();
	for(it=0;it<times;it++) {
		uRand=Random_Int();
		fIndex=uRand%max;
		if((Random_Byte()%27) == 0) {
		//	TRACE("gfs_Init...");
			gfs_Init(GFS_FILE_START,GFS_FILE_SIZE,GFS_FILE_HDNM,0);
		}
		//TRACE("uRand[%x]fIndex[%d]sMax[%d]",uRand,fIndex,sMax);
		if(tfileArr[fIndex].sName[0]=='\0' || tfileArr[fIndex].fLen == 0) {
			tfileArr[fIndex].times = 0;
			#ifdef GFS_RECORD_FUNTION
			if((Random_Byte()%17) == 5) {
				do {
					SaveLen =Random_DW() % (2048+1);
				}
				while(SaveLen < 4);
				
				do {
					offset =Random_DW() % (MAX_SAVE_FILE_LEN/SaveLen);
				}
				while(offset == 0);
				sprintf_s(tfileArr[fIndex].sName,sizeof(tfileArr[fIndex].sName),"%d-%d_%d.cnt",fIndex,SaveLen,offset);
				pfd=gfs_open(tfileArr[fIndex].sName,GFS_O_RDWR|GFS_O_CREAT|GFS_O_COUNT,SaveLen,offset);
				if(pfd==NULL) {
					LG(" CREAT_openc[%d] name[%s]Err-Len[%d]-num[%d]",fIndex,tfileArr[fIndex].sName,SaveLen,offset);
					tfileArr[fIndex].sName[0]='\0';
					continue;
				}
				Random_Buff(tfileArr[fIndex].pBuff,SaveLen);
				ret= contFileAuto(pfd,(u8*)tfileArr[fIndex].pBuff,SaveLen,(Random_Int() % 17)+1);
				if(ret > 0) {
					LG("CREATc gfs write[%s]Err,SaveLen[%d],ret[%d]",tfileArr[fIndex].sName,SaveLen,ret);
					errTimes++;
					break;
				}
				tfileArr[fIndex].flag = F_CONT;
				tfileArr[fIndex].num=offset;
				tfileArr[fIndex].fLen = SaveLen;
				
				//TRACE("11 fIndex[%d] SaveLen[%d]offset[%d]",fIndex,SaveLen,offset);
				gfs_close(pfd);
			}
			else
			#endif
			{
				ret=sprintf_s(tfileArr[fIndex].sName,sizeof(tfileArr[fIndex].sName),"%d-%ld",fIndex,Random_Int());
				pfd=gfs_open(tfileArr[fIndex].sName,GFS_O_RDWR|GFS_O_CREAT);
				if(pfd==NULL) {
					LG("CREAT_open[%d] name[%s]Err-[%d]-t->%d",fIndex,tfileArr[fIndex].sName,ret,it);
					tfileArr[fIndex].sName[0]='\0';
					continue;
				}
				offset = 0;
				sMax = Random_Byte()%64;
				for(si=0;si<sMax;si++) {
					if(offset > (MAX_SAVE_TREM_LEN -8)) break;
					do {
						SaveLen =Random_Int() % (MAX_SAVE_TREM_LEN-offset);
					}
					while(SaveLen == 0);
					Random_Buff(tfileArr[fIndex].pBuff+offset,SaveLen);
					totalSave += SaveLen;
					ret=gfs_write(pfd,tfileArr[fIndex].pBuff+offset,SaveLen);
					if(ret <= 0) {
						LG("CREAT0 gfs write[%s]Err,offset[%d],SaveLen[%d],ret[%d]",tfileArr[fIndex].sName,offset,SaveLen,ret);
						break;
					}
					if(ret < SaveLen) {
						LG("CREAT1 gfs write[%s]warning,offset[%d],SaveLen[%d],ret[%d]",tfileArr[fIndex].sName,offset,SaveLen,ret);
					}
					offset += ret;
					fileLen += ret;
				}
				tfileArr[fIndex].flag = 0;
				tfileArr[fIndex].fLen=offset;
				gfs_close(pfd);
			}
		}
		#ifdef GFS_RECORD_FUNTION
		else if(tfileArr[fIndex].flag & F_CONT) {
			oflag=Random_Byte();
			if(oflag%3) {
				pfd=gfs_open(tfileArr[fIndex].sName,GFS_O_RDWR|GFS_O_COUNT,tfileArr[fIndex].fLen,tfileArr[fIndex].num);
				if(pfd==NULL) {
					LG("cot##open[%d] name[%s][%d,%d]Err-[%d]-t->%d",fIndex,tfileArr[fIndex].sName,tfileArr[fIndex].fLen,tfileArr[fIndex].num,ret,it);
					errTimes++;
					continue;
				}
				offset = tfileArr[fIndex].fLen;
				pCheckbuff=tfileArr[fIndex].pBuff + offset;
				//TRACE("21fIndex[%d] SaveLen[%d]",fIndex,offset);
				ret=gfs_read(pfd,pCheckbuff,0);
				if(ret < offset) {
					LG("cot##gfs read1[%s],offset[%d],ret[%d] Err",tfileArr[fIndex].sName,offset,ret);
					errTimes++;
				}
				else {
					int j;
					u8 *pTag;
					pTag=tfileArr[fIndex].pBuff;
					for(j=0;j<offset;j++) {
						if(pCheckbuff[j] != pTag[j]) break;
					}
					if(j < offset) {
						LG("cot##read cmp1[%s],offset[%d][%d][%d != %d] Err",tfileArr[fIndex].sName,offset,j, pCheckbuff[j],pTag[j]);
						errTimes++;
					}
				}
				//TRACE("210 fIndex[%d] num[%d]",fIndex,tfileArr[fIndex].num);
				gfs_close(pfd);
				gfs_remove(tfileArr[fIndex].sName);
				tfileArr[fIndex].sName[0]='\0';
				tfileArr[fIndex].fLen=0;
			}
			else {
				pfd=gfs_open(tfileArr[fIndex].sName,GFS_O_RDWR|GFS_O_COUNT,tfileArr[fIndex].fLen,tfileArr[fIndex].num);
				if(pfd==NULL) {
					LG("s##open cont[%s][%d]Err->%d",tfileArr[fIndex].sName,tfileArr[fIndex].fLen,tfileArr[fIndex].times);
					errTimes++;
					continue;
				}
				//TRACE("22fIndex[%d] SaveLen[%d]",fIndex,tfileArr[fIndex].fLen);
				ret= contFileAuto(pfd,(u8*)tfileArr[fIndex].pBuff,tfileArr[fIndex].fLen,(Random_Int() % 100)+1);
				if(ret > 0) {
					LG("###cont FileAuto Err[%d]",ret);
					errTimes++;
				}
				//TRACE("220 fIndex[%d] num[%d]",fIndex,tfileArr[fIndex].num);
				gfs_close(pfd); 
			}
			tfileArr[fIndex].times++;
		}
		#endif
		else {	//Read----- Data
			oflag=Random_Byte();
			tfileArr[fIndex].times++;
			if((oflag&0x03)==1) {		// save 0
				SaveLen = 0;
				while(SaveLen<=0) SaveLen =Random_Int()% MAX_SAVE_TREM_LEN ;
				//TRACE("gfs open TRUNC[%s] fLen[%d]SaveLen[%d]",tfileArr[fIndex].sName,tfileArr[fIndex].fLen,SaveLen);

				pfd=gfs_open(tfileArr[fIndex].sName,GFS_O_RDWR|GFS_O_TRUNC);
				if(pfd==NULL) {
					LG("s##open TRUNC[%s][%d]Err->%d,flag[%d]",tfileArr[fIndex].sName,tfileArr[fIndex].fLen,tfileArr[fIndex].times,tfileArr[fIndex].flag);
					errTimes++;
					continue;
				}
				Random_Buff(tfileArr[fIndex].pBuff,SaveLen);
				ret=gfs_write(pfd,tfileArr[fIndex].pBuff,SaveLen);
				if(ret < SaveLen) {
					LG("TRUNC gfs write[%s][%d]warning,SaveLen[%d],ret[%d]",tfileArr[fIndex].sName,tfileArr[fIndex].fLen,SaveLen,ret);
				}
				if(ret > 0) {
					fileLen += ret;
					tfileArr[fIndex].fLen=ret;
					totalSave += SaveLen;
				}
				else {
					tfileArr[fIndex].sName[0] = '\0';
					tfileArr[fIndex].fLen=0;
				}
				gfs_close(pfd);	
				tfileArr[fIndex].flag=1;

			}
			else if((oflag&0x03)==2) {	//save add
				offset = tfileArr[fIndex].fLen;
				if(offset > (MAX_SAVE_TREM_LEN -8)) continue;
				SaveLen = 0;
				while(SaveLen<=0) SaveLen =Random_Int()% (MAX_SAVE_TREM_LEN-offset) ;

				pfd=gfs_open(tfileArr[fIndex].sName,GFS_O_RDWR|GFS_O_APPEND);
				if(pfd==NULL) {
					LG("s##open APPEND[%s][%d]Err->%d,flag[%d]",tfileArr[fIndex].sName,tfileArr[fIndex].fLen,tfileArr[fIndex].times,tfileArr[fIndex].flag);
					errTimes++;
					continue;
				}
				Random_Buff(tfileArr[fIndex].pBuff+offset,SaveLen);
				totalSave += SaveLen;
				ret=gfs_write(pfd,tfileArr[fIndex].pBuff+offset,SaveLen);
				if(ret < SaveLen) {
					LG("APPEND_write[%s]warning,offset[%d],SaveLen[%d],ret[%d]",tfileArr[fIndex].sName,offset,SaveLen,ret);
				}
				if(ret>0) {
					fileLen += ret;
					tfileArr[fIndex].fLen += ret;
				}
				gfs_close(pfd);
				tfileArr[fIndex].flag=2;
			}
			else if((oflag&0x03)==3) {		//save x
				u32 maxLen,offset;
				pfd=gfs_open(tfileArr[fIndex].sName,GFS_O_RDWR);
				if(pfd==NULL) {
					LG("s##open RDWR[%s][%d]times[%d],flag[%d]",tfileArr[fIndex].sName,tfileArr[fIndex].fLen,tfileArr[fIndex].times,tfileArr[fIndex].flag);
					errTimes++;
					continue;
				}	
				maxLen=gfs_GetSize(pfd);
				for(ets=0;ets<100;ets++) {
					offset = Random_Int()%(maxLen+1);
					if(offset > (MAX_SAVE_TREM_LEN -8)) continue;
					SaveLen =Random_Int()% (MAX_SAVE_TREM_LEN-offset);
					if(SaveLen <= 0) continue;
				}
				if(ets<100) {
					Random_Buff(pSavebuff,SaveLen);
					//TRACE("gfs open TRUNC[%s] fLen[%d]SaveLen[%d]",tfileArr[fIndex].sName,tfileArr[fIndex].fLen,SaveLen);
					gfs_seek(pfd,offset,GFS_SEEK_SET);
					totalSave += SaveLen;
					ret=gfs_write(pfd,pSavebuff,SaveLen);
					if(ret <= 0) {
						LG("RDWR gfs write[%s]Err,offset[%d],SaveLen[%d],maxLen[%d]offset[%d]ret[%d]",tfileArr[fIndex].sName,offset,SaveLen,maxLen,offset,ret);
					}
					else if(ret < SaveLen) {
						LG("RDWR write[%s]warning,offset[%d],SaveLen[%d],ret[%d]",tfileArr[fIndex].sName,offset,SaveLen,ret);
					}
					if(ret > 0) {		
						memcpy(tfileArr[fIndex].pBuff+offset,pSavebuff,ret);
						fileLen += ret;
						if(maxLen <(offset+ret))
							maxLen = (offset+ret);
						tfileArr[fIndex].fLen=maxLen;	
					}
					tfileArr[fIndex].flag=3;
				}
				gfs_close(pfd);	
			}
			else { // remove
				pfd=gfs_open(tfileArr[fIndex].sName,GFS_O_RDWR);
				if(pfd==NULL) {
					LG("e##gfs_open RDWR[%s][%d]Err->%d,flag[%d]",tfileArr[fIndex].sName , tfileArr[fIndex].fLen,tfileArr[fIndex].times,tfileArr[fIndex].flag);
					errTimes++;
					continue;
				}
				offset = tfileArr[fIndex].fLen;
				pCheckbuff=(u8*)malloc(offset);
				totalRead += offset;
				ret=gfs_read(pfd,pCheckbuff,offset);
				if(ret < offset) {//e##gfs_read[97/10605],offset[3603],ret[2048] Err
					LG("e##gfs_read[%s],offset[%d],ret[%d] Err->%d",tfileArr[fIndex].sName,offset,ret,tfileArr[fIndex].times);
					errTimes++;
				}
				else {
					int i;
					u8 *pRead,*pTag;
					pRead=pCheckbuff;
					pTag=tfileArr[fIndex].pBuff;
					for(i=0;i<offset;i++) {
						if(pRead[i] != pTag[i]) break;
					}
					if(i < offset) {
						LG("e##read cmp[%s],offset[%d,%d],ret[%d] Err->%d",tfileArr[fIndex].sName,i,offset,ret,tfileArr[fIndex].times);
						errTimes++;
					}
				}
				free(pCheckbuff);
				gfs_close(pfd);
				gfs_remove(tfileArr[fIndex].sName);
				tfileArr[fIndex].sName[0]='\0';
				tfileArr[fIndex].fLen=0;
			}
		}
	}
	time1=GetTickCount()-time1;
	LG("-check--totalSave[%d]fileLen[%d]totalRead[%d]gfs_erase_times[%d]",totalSave,fileLen,totalRead,gfs_erase_times);
	time2=GetTickCount();
	for(i=0;i<max;i++) {
		if(tfileArr[i].fLen > 0) {
			offset = tfileArr[i].fLen;
			if(offset <= 0) {
				LG("cot##gfs read2[%s],fLen[%d]Err",tfileArr[i].sName,offset);
				continue;
			}
			#ifdef GFS_RECORD_FUNTION
			if(tfileArr[i].flag & GFS_O_COUNT) {
				pfd=gfs_open(tfileArr[i].sName,GFS_O_RDWR|GFS_O_COUNT,tfileArr[i].fLen,tfileArr[i].num);
				totalRead += offset;
				pCheckbuff=tfileArr[i].pBuff + offset;
				//TRACE("3fIndex[%d] SaveLen[%d]",fIndex,SaveLen);
				ret=gfs_read(pfd,pCheckbuff,0);
				if(ret < offset) {
					LG("cot##gfs read3[%s],offset[%d],ret[%d] Err",tfileArr[i].sName,offset,ret);
					errTimes++;
				}
				else {
					int j;
					u8 *pTag;
					pTag=tfileArr[i].pBuff;
					for(j=0;j<offset;j++) {
						if(pCheckbuff[j] != pTag[j]) break;
					}
					if(j < offset) {
						LG("cot##read cmp2[%s],offset[%d][%d][%d != %d] Err",tfileArr[fIndex].sName,offset,j, pCheckbuff[j],pTag[j]);
						errTimes++;
					}
				}
				gfs_close(pfd);
			}
			else
			#endif
			{
				pfd=gfs_open(tfileArr[i].sName,GFS_O_RDONLY);
				if(pfd==NULL) {
					LG("e##gfs open RDONLY[%s][%d]Err->%d,flag[%d]",tfileArr[i].sName,tfileArr[i].fLen,tfileArr[fIndex].times,tfileArr[fIndex].flag);
					errTimes++;
					continue;
				}
				if(offset != gfs_GetSize(pfd)) {
					LG("e##gfs[%s] GetSize[%d] != Len[%d] Err",tfileArr[i].sName,gfs_GetSize(pfd),offset);
					gfs_close(pfd);
					errTimes++;
					continue;
				}
				pCheckbuff=(u8*)malloc(offset);
				if(pCheckbuff==NULL) {
					LG("gfs malloc(%d) Err",offset);
					gfs_close(pfd);
					continue;
				}
				totalRead += offset;
				ret=gfs_read(pfd,pCheckbuff,offset);
				if(ret < offset) {
					LG("e##gfs read[%s],offset[%d],ret[%d] Err",tfileArr[i].sName,offset,ret);
					errTimes++;
				}
				else {
					int j;
					u8 *pRead,*pTag;
					pRead=pCheckbuff;
					pTag=tfileArr[i].pBuff;
					for(j=0;j<offset;j++) {
						if(pRead[j] != pTag[j]) break;
					}
					if(j < offset) {
						LG("e##read cmp2[%s],offset[%d][%d][%d != %d] Err",tfileArr[fIndex].sName,offset,j,pRead[j],pTag[j]);
						errTimes++;
					}
				}
				free(pCheckbuff);
				gfs_close(pfd);
			}
			gfs_remove(tfileArr[i].sName);
		}
		//TRACE("i(%d)pBuff(%X)fLen(%d)", i, tfileArr[i].pBuff, tfileArr[i].fLen);
		free(tfileArr[i].pBuff); tfileArr[i].pBuff = NULL;
		tfileArr[i].sName[0]='\0';
		tfileArr[i].fLen=0;
	}
	time2=GetTickCount()-time2;
	LG("totalSave[%d]fileLen[%d]totalRead[%d] errTimes[%d] time-taken[%d,%d]erase_times[%d]",totalSave,fileLen,totalRead,errTimes,time1,time2,gfs_erase_times);

	gfs_checkALL();
	//gfs_Fomat(NULL);
	GenRandom_Free();

	sz_set_unit_int(pInOutPar, errTimes);
	return 1;
}







/*

int APP_TestSaveCont(char *pTitle)
{
	int i,timeAll,ret1,conMax;
	int timemax,timemin,ErrTime,testSize=247;
	u8 InitBuf[256],sBuff[256],ukey[8];
	InitBuf[0]=0;
	if(APP_InputNum("conMax","recvLen(1~64512)",NULL,(char*)InitBuf,1,5)) {
		return 0;
	}
	conMax=API_atoi((char*)InitBuf);	
	void* fd=APP_OpenChangeMsg(pTitle);
	for(i=0;i<sizeof(sBuff);i++) {
		sBuff[i]=i;
	}
	APP_ShowChangeMsg(fd,"Writing Record[%d]",conMax);
	TRACE("Ordinary Writing Record %d...",conMax);
	PLBlRc pBal;
	{
		int timeMs = OsGetTickCount();
		pBal = cBalancedRecordInit(testSize,conMax);
		timeMs = OsGetTickCount() - timeMs;
		TRACE("BalancedRecord Init[%x]timeMs=%d...",pBal,timeMs);
	}
	APP_ShowChangeMsg(fd,"BalancedRecord Init[%X]",pBal);
	if(pBal == NULL) return 0;
	timeAll = 0;
	timemax=0;
	timemin=0x7FFFFFFF;
	for(i=0;i<conMax;i++) {
		sBuff[0]= i/256;
		sBuff[1]= i&0xff;
		int timeMs = OsGetTickCount();
		ret1 = BalancedRecordSave(pBal,sBuff+(sizeof(sBuff)-testSize));
		timeMs = OsGetTickCount() - timeMs;
		if(ret1 < testSize) {
			TRACE("[%d]BalancedRecordSave[%d] timeMs[%d] Err",i,ret1,timeMs);
			break;
		}
		if(timemax < timeMs) timemax = timeMs;
		if(timemin > timeMs) timemin = timeMs;
		timeAll += timeMs;
		//APP_ShowChangeMsg(fd,"璁板綍[%d->%d]",i,timeMs);
		TRACE("record[%d->%d]",i,timeMs);
	}
	APP_ShowChangeMsg(fd,"Total[%d]time[%d][%d,%d]",i,timeAll,timemax,timemin);
	TRACE("Save:Total cont[%d]total time[%d] max=%d,min=%d",i,timeAll,timemax,timemin);
	APP_WaitUiEvent(10*1000);

	APP_ShowChangeMsg(fd,"Roll over rewrite[%d]",conMax);
	TRACE("Roll over rewrite %d...",conMax);
	OsGetSrand(OsGetTickCount());
	for(i=0;i<sizeof(InitBuf);i++) {
		InitBuf[i]=OsGetRand();
	}
	for(i=0;i<sizeof(ukey);i++) {
		ukey[i]=OsGetRand();
	}
	timeAll = 0;
	timemax=0;
	timemin=0x7FFFFFFF;
	TRACE_HEX("ukey", ukey, sizeof(ukey));
	TRACE_HEX("Save InitBuf", InitBuf, sizeof(InitBuf));
	for(i=0;i<conMax;i++) {
		for(int j=0;j<sizeof(InitBuf);j += 8) {
			API_Crypt(CRYPT_TYPE_DES,ukey,8,NULL,InitBuf+j,8,InitBuf+j,NULL,CRYPT_MODE_ENCRYPT|CRYPT_MODE_ECB);
		}
		int timeMs = OsGetTickCount();
		ret1 = BalancedRecordSave(pBal,InitBuf +(sizeof(sBuff)-testSize));
		timeMs = OsGetTickCount() - timeMs;
		if(ret1 < testSize) {
			TRACE("[%d]BalancedRecordSave[%d] timeMs[%d] Err",i,ret1,timeMs);
			break;
		}
		if(timemax < timeMs) timemax = timeMs;
		if(timemin > timeMs) timemin = timeMs;
		timeAll += timeMs;
		//APP_ShowChangeMsg(fd,"璁板綍[%d->%d]",i,timeMs);
		TRACE("record[%d->%d]",i,timeMs);
	}
	APP_ShowChangeMsg(fd,"Total[%d]time[%d][%d,%d]",i,timeAll,timemax,timemin);
	TRACE("record:Total cont[%d]total time[%d] max=%d,min=%d\r\n",i,timeAll,timemax,timemin);
	APP_WaitUiEvent(10*1000);


	APP_ShowChangeMsg(fd,"read test[%d]",conMax);
	TRACE("read test[%d]...",conMax);
	timeAll = 0;
	ErrTime = 0;
	timemax=0;
	timemin=0x7FFFFFFF;
	int PreviousIndex = 0;
	TRACE_HEX("Check InitBuf", InitBuf, sizeof(InitBuf));
	for(i=0;i<conMax;i++) {
		memset(sBuff,0x00,sizeof(sBuff));
		int timeMs = OsGetTickCount();
		while(1) {
			ret1 = BalancedRecordRead(pBal,PreviousIndex,sBuff +(sizeof(sBuff)-testSize));
			PreviousIndex++;
			if(ret1 < testSize) {
				TRACE("[%d->%d]BalancedRecordRead[%d] timeMs[%d] Err",i,PreviousIndex,ret1,timeMs);
				continue;
			}
			break;
		}	
		timeMs = OsGetTickCount() - timeMs;
		
		if(memcmp(InitBuf+(sizeof(sBuff)-testSize),sBuff+(sizeof(sBuff)-testSize),testSize) == 0) {
			ret1 = TRUE; 
		}
		else {
			ret1 = FALSE;
			ErrTime++;
		}
		for(int j=0;j<sizeof(InitBuf);j += 8) {
			API_Crypt(CRYPT_TYPE_DES,ukey,8,NULL,InitBuf+j,8,InitBuf+j,NULL,CRYPT_MODE_DECRYPT|CRYPT_MODE_ECB);
		}
		if(timemax < timeMs) timemax = timeMs;
		if(timemin > timeMs) timemin = timeMs;
		timeAll += timeMs;
		//APP_ShowChangeMsg(fd,"Read[%d->%d]->%d",i,timeMs,ret2);
		TRACE("Read[%d->%d]->%d",i,timeMs,ret1);
	}
	APP_ShowChangeMsg(fd,"total[%d]time[%d][%d][%d,%d]",i,timeAll,ErrTime,timemax,timemin);
	TRACE("Read:Total cont[%d]total time[%d] err=%d,max=%d,min=%d",i,timeAll,ErrTime,timemax,timemin);
	APP_WaitUiEvent(10*1000);
	BalancedRecordClean(pBal);
	APP_CloseChangeMsg(fd);
	return 0;
}


int APP_TestSaveCont(char *pTitle)
{
	int i,timeAll,ret1,ret2,conMax=3000;
	int timemax,timemin,ErrTime;
	u8 InitBuf[256],sBuff[256];
	void* fd=APP_OpenChangeMsg(pTitle);
	for(i=0;i<256;i++) {
		sBuff[i]=i;
	}
	APP_ShowChangeMsg(fd,"鏅€氶娆¤褰昜%d]",conMax);
	TRACE("Ordinary Writing Record %d...\r\n",conMax);
	timeAll = 0;
	timemax=0;
	timemin=0x7FFFFFFF;
	for(i=0;i<conMax;i++) {
		sBuff[0]= i/256;
		sBuff[1]= i&0xff;
		int timeMs = API_TimeCurrMS();
		gFILE pFd = API_fopen("cData.bin",F_RDWR|F_CREAT|F_APPEND);
		if(GFD_CHECK_ERR(pFd)) {
			TRACE("[%d]fopen[%x] Err\r\n",i,pFd);
			break;
		}
		ret1 = API_fwrite(pFd,sBuff,sizeof(sBuff));
		ret2 = API_fclose(pFd);
		if(ret1 < sizeof(sBuff) || ret2 != 0) {
			TRACE("[%d]fwrite[%d] fclose[%d] Err\r\n",i,ret1,ret2);
			break;
		}
		timeMs = API_TimeCurrMS() - timeMs;
		if(timemax < timeMs) timemax = timeMs;
		if(timemin > timeMs) timemin = timeMs;
		timeAll += timeMs;
		//APP_ShowChangeMsg(fd,"璁板綍[%d->%d]",i,timeMs);
		TRACE("record[%d->%d]\r\n",i,timeMs);
	}
	APP_ShowChangeMsg(fd,"鎬昏[%d]鎬绘椂[%d][%d,%d]",i,timeAll,timemax,timemin);
	TRACE("Save:Total cont[%d]total time[%d] max=%d,min=%d\r\n",i,timeAll,timemax,timemin);
	APP_WaitUiEvent(10*1000);

	APP_ShowChangeMsg(fd,"閲嶅啓婊氬瓨[%d]",conMax);
	TRACE("Roll over rewrite %d...\r\n",conMax);

	
	API_Srand(API_TimeCurrMS());
	for(i=0;i<sizeof(InitBuf);i++) {
		InitBuf[i]=API_Rand();
	}
	timeAll = 0;
	timemax=0;
	timemin=0x7FFFFFFF;
	for(i=0;i<sizeof(InitBuf);i++) {
		sBuff[i]=InitBuf[i];
	}
	TRACE_HEX("Save sBuff", sBuff, sizeof(sBuff));
	for(i=0;i<conMax;i++) {
		for(int j=0;j<sizeof(sBuff);j += 8) {
			API_Crypt(CRYPT_TYPE_DES,sBuff+j,8,NULL,sBuff+j,8,sBuff+j,NULL,CRYPT_MODE_ENCRYPT|CRYPT_MODE_ECB);
		}
		int timeMs = API_TimeCurrMS();
		gFILE pFd = API_fopen("cData.bin",F_RDWR|F_CREAT|F_APPEND);
		if(GFD_CHECK_ERR(pFd)) {
			TRACE("[%d]fopen[%x] Err\r\n",i,pFd);
			break;
		}
		API_fseek(pFd,i*256,F_SEEK_SET);
		ret1 = API_fwrite(pFd,sBuff,sizeof(sBuff));
		ret2 = API_fclose(pFd);
		if(ret1 < sizeof(sBuff) || ret2 != 0) {
			TRACE("[%d]fwrite[%d] fclose[%d] Err\r\n",i,ret1,ret2);
			break;
		}
		timeMs = API_TimeCurrMS() - timeMs;
		if(timemax < timeMs) timemax = timeMs;
		if(timemin > timeMs) timemin = timeMs;
		timeAll += timeMs;
		//APP_ShowChangeMsg(fd,"璁板綍[%d->%d]",i,timeMs);
		TRACE("record[%d->%d]\r\n",i,timeMs);
	}
	APP_ShowChangeMsg(fd,"鎬昏[%d]鎬绘椂[%d][%d,%d]",i,timeAll,timemax,timemin);
	TRACE("record:Total cont[%d]total time[%d] max=%d,min=%d\r\n",i,timeAll,timemax,timemin);
	APP_WaitUiEvent(10*1000);


	APP_ShowChangeMsg(fd,"璇绘祴璇昜%d]",conMax);
	TRACE("read test[%d]...\r\n",conMax);
	timeAll = 0;
	ErrTime = 0;
	timemax=0;
	timemin=0x7FFFFFFF;
	
	TRACE_HEX("InitBuf", InitBuf, sizeof(InitBuf));
	for(i=0;i<conMax;i++) {
		for(int j=0;j<sizeof(InitBuf);j += 8) {
			API_Crypt(CRYPT_TYPE_DES,InitBuf+j,8,NULL,InitBuf+j,8,InitBuf+j,NULL,CRYPT_MODE_ENCRYPT|CRYPT_MODE_ECB);
		}
		memset(sBuff,0x00,sizeof(sBuff));
		int timeMs = API_TimeCurrMS();
		gFILE pFd = API_fopen("cData.bin",F_RDWR|F_CREAT|F_APPEND);
		if(GFD_CHECK_ERR(pFd)) {
			TRACE("[%d]fopen[%x] Err\r\n",i,pFd);
			break;
		}
		API_fseek(pFd,i*256,F_SEEK_SET);
		ret1 = API_fread(pFd,sBuff,sizeof(sBuff));
		ret2 = API_fclose(pFd);
		if(ret1 < sizeof(sBuff) || ret2 != 0) {
			TRACE("[%d]fread[%d] fclose[%d] Err\r\n",i,ret1,ret2);
			break;
		}
		timeMs = API_TimeCurrMS() - timeMs;
		if(timemax < timeMs) timemax = timeMs;
		if(timemin > timeMs) timemin = timeMs;
		timeAll += timeMs;

		if(memcmp(InitBuf,sBuff,sizeof(sBuff)) == 0) {
			ret2 = TRUE; 
		}
		else {
			ret2 = FALSE;
			ErrTime++;
		}
		APP_ShowChangeMsg(fd,"璇诲彇[%d->%d]->%d",i,timeMs,ret2);
		TRACE("Read[%d->%d]->%d\r\n",i,timeMs,ret2);
	}
	APP_ShowChangeMsg(fd,"鎬昏[%d]鎬绘椂[%d][%d][%d,%d]",i,timeAll,ErrTime,timemax,timemin);
	TRACE("Read:Total cont[%d]total time[%d] err=%d,max=%d,min=%d\r\n",i,timeAll,ErrTime,timemax,timemin);

	APP_WaitUiEvent(10*1000);
	API_fremove("cData.bin");
	APP_CloseChangeMsg(fd);
	return 0;
}


int APP_TestSaveCont2(char *pTitle)
{
	int i,timeAll,ret1,ret2,conMax=3000;
	int timemax,timemin,ErrTime;
	u8 sBuff[256];
	void* fd=APP_OpenChangeMsg(pTitle);
	for(i=0;i<256;i++) {
		sBuff[i]=i;
	}
	
	APP_ShowChangeMsg(fd,"鏅€氶娆¤褰昜%d]",conMax);
	
	timeAll = 0;
	timemax=0;
	timemin=0x7FFFFFFF;
	timeAll = API_TimeCurrMS();
	gFILE pFd = API_fopen("cdata.cnt",F_RDWR|F_CREAT|F_COUNT,sizeof(sBuff),3000);
	if(GFD_CHECK_ERR(pFd)) {
		TRACE("[%d]fopen[%x] Err\r\n",i,pFd);
		return 0;
	}
	timeAll = API_TimeCurrMS() - timeAll;
	TRACE("Ordinary Writing[6000] open time=%dms...\r\n",timeAll);
	for(i=0;i<conMax;i++) {
		sBuff[0]= i/256;
		sBuff[1]= i&0xff;
		int timeMs = API_TimeCurrMS();
		ret1 = API_fwrite(pFd,sBuff,0); //Power outage is effective
		if(ret1 < sizeof(sBuff) ) {
			TRACE("[%d]fwrite[%d] Err\r\n",i,ret1);
			break;
		}
		timeMs = API_TimeCurrMS() - timeMs;
		if(timemax < timeMs) timemax = timeMs;
		if(timemin > timeMs) timemin = timeMs;
		timeAll += timeMs;
		//APP_ShowChangeMsg(fd,"璁板綍[%d->%d]",i,timeMs);
		TRACE("record[%d->%d]\r\n",i,timeMs);
	}
	APP_ShowChangeMsg(fd,"鎬昏[%d]鎬绘椂[%d][%d,%d]",i,timeAll,timemax,timemin);
	TRACE("Save:Total cont[%d]total time[%d] max=%d,min=%d\r\n",i,timeAll,timemax,timemin);
	APP_WaitUiEvent(10*1000);
	timeAll = 0;
	timemax=0;
	timemin=0x7FFFFFFF;
	sBuff[0]= 0xFF;
	sBuff[1]= 0xFF;
	for(i=0;i<conMax;i++) {
		sBuff[2]= i/256;
		sBuff[3]= i&0xff;
		int timeMs = API_TimeCurrMS();
		ret1 = API_fwrite(pFd,sBuff,0); //Power outage is effective
		if(ret1 < sizeof(sBuff) ) {
			TRACE("[%d]fwrite[%d] Err\r\n",i,ret1);
			break;
		}
		timeMs = API_TimeCurrMS() - timeMs;
		if(timemax < timeMs) timemax = timeMs;
		if(timemin > timeMs) timemin = timeMs;
		timeAll += timeMs;
		//APP_ShowChangeMsg(fd,"璁板綍[%d->%d]",i,timeMs);
		TRACE("record[%d->%d]\r\n",i,timeMs);
	}
	API_fclose(pFd);
	APP_ShowChangeMsg(fd,"鎬昏[%d]鎬绘椂[%d][%d,%d]",i,timeAll,timemax,timemin);
	TRACE("record:Total cont[%d]total time[%d] max=%d,min=%d\r\n",i,timeAll,timemax,timemin);
	APP_WaitUiEvent(10*1000);
	


	APP_ShowChangeMsg(fd,"璇绘祴璇昜%d]",conMax);
	TRACE("read test[%d]...\r\n",conMax);
	ErrTime = 0;
	timemax=0;
	timemin=0x7FFFFFFF;
	timeAll = API_TimeCurrMS();
	pFd = API_fopen("cdata.cnt",F_RDWR|F_CREAT|F_COUNT,sizeof(sBuff),3000);
	if(GFD_CHECK_ERR(pFd)) {
		TRACE("[%d]fopen[%x] Err\r\n",i,pFd);
		return 0;
	}
	timeAll = API_TimeCurrMS() - timeAll;
	TRACE("Ordinary read[3000] open time=%dms...\r\n",timeAll);
	for(i=0;i<conMax;i++) {
		memset(sBuff,0x00,sizeof(sBuff));
		int timeMs = API_TimeCurrMS();
		ret1 = API_fread(pFd,sBuff,i);
		if(ret1 < sizeof(sBuff)) {
			TRACE("[%d]fread[%d] fclose[%d] Err\r\n",i,ret1,ret2);
			break;
		}
		timeMs = API_TimeCurrMS() - timeMs;
		if(timemax < timeMs) timemax = timeMs;
		if(timemin > timeMs) timemin = timeMs;
		timeAll += timeMs;
		
		int index = 256*sBuff[2] + sBuff[3];
		if(index == (conMax - i - 1)) {
			ret2 = TRUE; 
		}
		else {
			ret2 = FALSE;
			ErrTime++;
		}
		//APP_ShowChangeMsg(fd,"璇诲彇[%d->%d]->%d",i,timeMs,ret2);
		TRACE("Read[%d->%d]->%d[%d,%d]\r\n",i,timeMs,ret2,index,(conMax - i - 1));
	}
	APP_ShowChangeMsg(fd,"鎬昏[%d]鎬绘椂[%d][%d][%d,%d]",i,timeAll,ErrTime,timemax,timemin);
	TRACE("Read:Total cont[%d]total time[%d] err=%d,max=%d,min=%d\r\n",i,timeAll,ErrTime,timemax,timemin);
	API_fclose(pFd);

	APP_WaitUiEvent(10*1000);
	API_fremove("cdata.cnt");
	
	APP_CloseChangeMsg(fd);
	return 0;
}
*/

//=====================配置==================================================
//=========函数对应表(名称,功能，使用规则，最后以NULL结束)=================
static const SzFuncTable gFuncTbl =
{
	FUNC_TYPE_I,	1,
	(1<<16)|(0<<8)|(10), //ver:1.0.10
	"fs",
	{
		"tinit",		EXP_TreeFileInit,
		"trun",			EXP_TreeFileRun,
		"tauto",		EXP_TreeFileAuto,
		"sinit", 		EXP_FileSysInit,
		"sdeinit",		EXP_FileSysDeinit,
		"sopen",		EXP_FileSysOpen,
		"swrite",		EXP_FileSysWrite,
		"sread",		EXP_FileSysRead,
		"sseek",		EXP_FileSysSeek,
		"sclose",		EXP_FileSysClose,
		"srename",		EXP_FileSysRename,
		"sremove",		EXP_FileSysRemove,
		"scheck",		EXP_FileSysCheck,
		"sauto",		EXP_FileSysAutoTest,
		NULL,		NULL,	//结束行
	}
};
/*
开发者信息(JSON格式{id:var,id2:var2,....})说明：
ID 	：已注册的开发者编号（版权依据，贡献依据，收益凭证），没有可以无此项
公司	：个人开发者不需要填此项
作者	：开发者姓名
手机	：开发者手机号（贡献版权依据）
*/
const char sDeveloperInfo[] = "gfs_test";


//==============导出总接口：gTotalInterSet==================
extern const SzImportSystem gTotalInterSet =
{
	sDeveloperInfo,
	NULL, //开发者证书
	{
		&gFuncTbl,
		NULL,	//结束行
	}
};

/*



*/


